﻿<UserControl x:Class="CodeCounsel.Office.DatabindingToolkit.WPFTaskPane"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:src="clr-namespace:CodeCounsel.Office.DatabindingToolkit">
    <UserControl.Resources>      
        <!-- The list to fill the control context menu -->
        <src:ControlTypes x:Key="controlTypesList" />
        <!-- Style applied to buttons to make them appear as a link button-->
        <Style x:Key="LinkButtonStyle" TargetType="{x:Type Button}">
            <Setter Property="SnapsToDevicePixels" Value="true" />
            <Setter Property="OverridesDefaultStyle" Value="true" />
            <Setter Property="Template">
                <Setter.Value>
                    <ControlTemplate TargetType="Button">
                        <TextBlock Background="{x:Null}" Cursor="Hand" 
                                   TextDecorations="Underline" TextWrapping="Wrap" >                  
                            <ContentPresenter VerticalAlignment="Center" RecognizesAccessKey="true"/> 
                        </TextBlock>
                        <ControlTemplate.Triggers>
                            <Trigger Property="IsPressed" Value="true" >
                                <Setter Property="Cursor" Value="Hand" />
                                <Setter Property="BitmapEffect">
                                    <Setter.Value>
                                        <DropShadowBitmapEffect ShadowDepth="1" Direction="330" Color="Black" Opacity="0.5" Softness="0.25" />
                                    </Setter.Value>
                                </Setter>
                            </Trigger>
                        </ControlTemplate.Triggers>
                    </ControlTemplate>
                </Setter.Value>
            </Setter>
        </Style>
        <ControlTemplate x:Key="dragPopup">
            <Popup Placement="MousePoint" AllowsTransparency="True" Opacity="20">
                <Border BorderBrush="Black" Background="LightBlue" CornerRadius="12" Padding="10,10,10,10">
                    <Grid>
                        <Grid.ColumnDefinitions>
                            <ColumnDefinition/>
                            <ColumnDefinition/>
                        </Grid.ColumnDefinitions>
                        <Grid.RowDefinitions>
                            <RowDefinition/>
                            <RowDefinition/>
                            <RowDefinition/>
                        </Grid.RowDefinitions>
                        <TextBlock Grid.Row="0" Grid.Column="0" Text="Node type: "/>
                        <TextBlock Grid.Row="0" Grid.Column="1" Text="{Binding ProposedType.WordType}"/>
                        <TextBlock Grid.Row="1" Grid.Column="0" Text="XPath: "/>
                        <TextBlock Grid.Row="1" Grid.Column="1" Text="{Binding XPath}"/>
                        <TextBlock Grid.Row="2" Grid.Column="0" Text="Namespace URI: "/>
                        <TextBlock Grid.Row="2" Grid.Column="1" Text="{Binding NamespaceUri}"/>
                    </Grid>
                </Border>
            </Popup>
        </ControlTemplate>
        <HierarchicalDataTemplate x:Key="normalNode" ItemsSource="{Binding ChildNodes}">
            <StackPanel Orientation="Horizontal" Height="18">
                <Image  Height="16" Width="16" VerticalAlignment="Center" 
                    Margin="0,0,3,0" Source="{Binding ProposedType.Image}">
                </Image>
                <TextBlock  Text="{Binding Name}"  VerticalAlignment="Center" 
                    Height="18" Padding="3,3,3,0" TextAlignment="Center" />
            </StackPanel>
        </HierarchicalDataTemplate>
        <HierarchicalDataTemplate x:Key="selectedNode" ItemsSource="{Binding ChildNodes}">
            <StackPanel Orientation="Horizontal" Height="18">
                <Image Height="16" Width="16" VerticalAlignment="Center"
                    Margin="0,0,3,0" Source="{Binding ProposedType.Image}">
                </Image>
                <Grid Height="18">
                    <Rectangle Stroke="Black" StrokeThickness="1" StrokeDashArray="3" Fill="LightBlue" />
                    <TextBlock Text="{Binding Name}"
                               GiveFeedback="CustomXmlTreeViewNode_GiveFeedback" 
                               MouseLeftButtonDown="CustomXmlTreeViewNode_MouseLeftButtonDown"  
                               VerticalAlignment="Center" Height="18" Foreground="Black" Padding="3,3,3,0" />
                </Grid>
                <Button Height="18" Width="18" Click="ShowControlTypeContextMenuButton_Click" >
                    <Button.Template>
                        <ControlTemplate>
                            <Canvas>
                                <Rectangle Stroke="Gray" Height="18" Width="18" />
                                <Rectangle Stroke="White" Height="16" Width="16" Canvas.Left="1" Canvas.Top="1">
                                    <Rectangle.Fill>
                                        <LinearGradientBrush StartPoint="0,1" EndPoint="0,0">
                                            <GradientBrush.GradientStops>
                                                <GradientStop Color="LightGray" Offset="0" />
                                                <GradientStop Color="White" Offset="1" />
                                            </GradientBrush.GradientStops>
                                        </LinearGradientBrush>
                                    </Rectangle.Fill>
                                </Rectangle>
                                <Polygon Points="6,8 9,12 12,8" Fill="Black"/>
                            </Canvas>
                        </ControlTemplate>
                    </Button.Template>
                    <Button.ContextMenu>
                        <ContextMenu IsEnabled="{Binding HasChildNodes}" 
                                     MenuItem.Click="ShowControlTypeContextMenu_MenuItemClick"
                                     ItemsSource="{StaticResource controlTypesList}">
                            <ContextMenu.ItemTemplate>
                                <DataTemplate>
                                    <TextBlock Text="{Binding Title}"/>
                                </DataTemplate>
                            </ContextMenu.ItemTemplate>
                        </ContextMenu>
                    </Button.ContextMenu>
                </Button>
            </StackPanel>
        </HierarchicalDataTemplate>
        <Style x:Key="TreeViewItemFocusVisual">
            <Setter Property="Control.Template">
                <Setter.Value>
                    <ControlTemplate>
                        <Rectangle/>
                    </ControlTemplate>
                </Setter.Value>
            </Setter>
        </Style>
        <PathGeometry x:Key="TreeArrow" Figures="M0,0 L0,6 L6,0 z"/>
        <Style x:Key="ExpandCollapseToggleStyle" TargetType="{x:Type ToggleButton}">
            <Setter Property="Focusable" Value="False"/>
            <Setter Property="Width" Value="16"/>
            <Setter Property="Height" Value="16"/>
            <Setter Property="Template">
                <Setter.Value>
                    <ControlTemplate TargetType="{x:Type ToggleButton}">
                        <Border Width="16" Height="16" Background="Transparent" Padding="5,5,5,5">
                            <Path Fill="Transparent" Stroke="#FF989898" x:Name="ExpandPath" Data="{StaticResource TreeArrow}">
                                <Path.RenderTransform>
                                    <RotateTransform Angle="135" CenterX="3" CenterY="3"/>
                                </Path.RenderTransform>
                            </Path>
                        </Border>
                        <ControlTemplate.Triggers>
                            <Trigger Property="IsMouseOver" Value="True">
                                <Setter Property="Stroke" TargetName="ExpandPath" Value="#FF1BBBFA"/>
                                <Setter Property="Fill" TargetName="ExpandPath" Value="Transparent"/>
                            </Trigger>
                            <Trigger Property="IsChecked" Value="True">
                                <Setter Property="RenderTransform" TargetName="ExpandPath">
                                    <Setter.Value>
                                        <RotateTransform Angle="180" CenterX="3" CenterY="3"/>
                                    </Setter.Value>
                                </Setter>
                                <Setter Property="Fill" TargetName="ExpandPath" Value="#FF595959"/>
                                <Setter Property="Stroke" TargetName="ExpandPath" Value="#FF262626"/>
                            </Trigger>
                        </ControlTemplate.Triggers>
                    </ControlTemplate>
                </Setter.Value>
            </Setter>
        </Style>
        <Style x:Key="TreeViewItemStyle1" TargetType="{x:Type TreeViewItem}">
            <Setter Property="Background" Value="Transparent"/>
            <Setter Property="HorizontalContentAlignment" Value="{Binding Path=HorizontalContentAlignment, RelativeSource={RelativeSource AncestorType={x:Type ItemsControl}}}"/>
            <Setter Property="VerticalContentAlignment" Value="{Binding Path=VerticalContentAlignment, RelativeSource={RelativeSource AncestorType={x:Type ItemsControl}}}"/>
            <Setter Property="Padding" Value="1,0,0,0"/>
            <Setter Property="Foreground" Value="{DynamicResource {x:Static SystemColors.ControlTextBrushKey}}"/>
            <Setter Property="FocusVisualStyle" Value="{StaticResource TreeViewItemFocusVisual}"/>
            <Setter Property="Template">
                <Setter.Value>
                    <ControlTemplate TargetType="{x:Type TreeViewItem}">
                        <Grid>
                            <Grid.ColumnDefinitions>
                                <ColumnDefinition MinWidth="19" Width="Auto"/>
                                <ColumnDefinition Width="Auto"/>
                                <ColumnDefinition Width="*"/>
                            </Grid.ColumnDefinitions>
                            <Grid.RowDefinitions>
                                <RowDefinition Height="Auto"/>
                                <RowDefinition/>
                            </Grid.RowDefinitions>
                            <ToggleButton x:Name="Expander" Style="{StaticResource ExpandCollapseToggleStyle}" ClickMode="Press" IsChecked="{Binding Path=IsExpanded, RelativeSource={RelativeSource TemplatedParent}}"/>
                            <Border SnapsToDevicePixels="true" x:Name="Bd" Grid.Column="1" Background="{TemplateBinding Background}" BorderBrush="{TemplateBinding BorderBrush}" BorderThickness="{TemplateBinding BorderThickness}" Padding="{TemplateBinding Padding}">
                                <ContentPresenter SnapsToDevicePixels="{TemplateBinding SnapsToDevicePixels}" HorizontalAlignment="{TemplateBinding HorizontalContentAlignment}" x:Name="PART_Header" ContentSource="Header"/>
                            </Border>
                            <ItemsPresenter x:Name="ItemsHost" Grid.Column="1" Grid.ColumnSpan="2" Grid.Row="1"/>
                        </Grid>
                        <ControlTemplate.Triggers>
                            <Trigger Property="IsExpanded" Value="false">
                                <Setter Property="Visibility" TargetName="ItemsHost" Value="Collapsed"/>
                            </Trigger>
                            <Trigger Property="HasItems" Value="false">
                                <Setter Property="Visibility" TargetName="Expander" Value="Hidden"/>
                            </Trigger>
                            <Trigger Property="IsSelected" Value="true">
                                <Setter Property="Foreground" Value="{DynamicResource {x:Static SystemColors.HighlightTextBrushKey}}"/>
                            </Trigger>
                            <MultiTrigger>
                                <MultiTrigger.Conditions>
                                    <Condition Property="IsSelected" Value="true"/>
                                    <Condition Property="IsSelectionActive" Value="false"/>
                                </MultiTrigger.Conditions>
                                <Setter Property="Foreground" Value="{DynamicResource {x:Static SystemColors.ControlTextBrushKey}}"/>
                            </MultiTrigger>
                            <Trigger Property="IsEnabled" Value="false">
                                <Setter Property="Foreground" Value="{DynamicResource {x:Static SystemColors.GrayTextBrushKey}}"/>
                            </Trigger>
                        </ControlTemplate.Triggers>
                    </ControlTemplate>
                </Setter.Value>
            </Setter>
        </Style>
    </UserControl.Resources>
    <DockPanel>
        <TextBlock DockPanel.Dock="Top">Select a custom XML part:</TextBlock>
        <ComboBox DockPanel.Dock="Top" ItemsSource="{Binding XmlParts}" x:Name="customXmlPartsList">
            <ComboBox.ItemTemplate>
                <DataTemplate>
                    <StackPanel>
                        <TextBlock Text="{Binding ID}" />
                        <StackPanel Orientation="Horizontal">
                            <TextBlock Text="{Binding NamespaceUri}" />
                            <TextBlock>
                                <TextBlock.Style>
                                    <Style>
                                        <Style.Triggers>
                                            <DataTrigger Binding="{Binding NamespaceUri}" Value="{x:Null}">
                                                <DataTrigger.Setters>
                                                    <Setter Property="TextBlock.Text" Value="No namespace" />
                                                    <Setter Property="TextBlock.FontStyle" Value="{x:Static FontStyles.Italic}" />
                                                </DataTrigger.Setters>    
                                            </DataTrigger>
                                            <DataTrigger Binding="{Binding NamespaceUri}" Value="">
                                                <DataTrigger.Setters>
                                                    <Setter Property="TextBlock.Text" Value="No namespace" />
                                                    <Setter Property="TextBlock.FontStyle" Value="{x:Static FontStyles.Italic}" />
                                                </DataTrigger.Setters>    
                                            </DataTrigger>
                                        </Style.Triggers>
                                    </Style>
                                </TextBlock.Style>
                            </TextBlock>
                        </StackPanel>
                    </StackPanel>
                </DataTemplate>
            </ComboBox.ItemTemplate>
        </ComboBox>
        <TextBlock DockPanel.Dock="Top" TextWrapping="Wrap">The following elements can be applied to the document:</TextBlock>
        <Button DockPanel.Dock="Bottom" x:Name="refreshButton" Style="{StaticResource LinkButtonStyle}" 
            Foreground="Blue" Height="18">Refresh</Button>
        <Button DockPanel.Dock="Bottom" x:Name="insertXmlPartButton" Style="{StaticResource LinkButtonStyle}" 
            Foreground="Blue" Height="18"
            Click="insertXmlPartButton_Click">Insert a new custom XML part</Button>
        <TreeView x:Name="customXmlTreeView" 
            MinHeight="200"
            ItemsSource="{Binding ElementName=customXmlPartsList, Path=SelectedItem.RootNodes}"
            ItemTemplate="{StaticResource normalNode}"
            ItemContainerStyle="{DynamicResource TreeViewItemStyle1}"
            TreeViewItem.Selected="CustomXmlTreeView_TreeViewItemSelected" />
    </DockPanel>
</UserControl>
